#!/bin/sh

#___________________________________________________________
#
#  File:    addUser
#  By:      Ashmeet Sidana and Matt Ho
#  Date:    7/23/95
#  Purpose: Add a user to the Outbox system.
#___________________________________________________________
#

#
# compute users login name
#

UserName=`/usr/bin/whoami`

#
# Set some globals
#

ROOT="/var/www"
DOCROOT="$ROOT/htdocs"
TOGIF='/usr/sbin/togif'
GREP='/sbin/grep'
AWK='/usr/bin/awk'
NAME=`$GREP "^$UserName:" /etc/passwd | $AWK -F: '{print $5}' | $AWK -F, '{print $1}'`
if [ ! "$NAME"  ] ; then
    NAME=$UserName ;
fi

#
# make running addUser.sh "safe" over multiple runs
#

grep "<a href=\"/\~$UserName\">$UserName</a>" $DOCROOT/userList.html >/dev/null 2>&1
if [ $? -eq 0 ] ; then
   # User has already been added to the system file
   exit
fi


#
# compute hostname
#

HOST=`/usr/bsd/hostname`

#_____________________________________
#
#	make the UserNames default home page
#	along with a Public directory
#_____________________________________

if test -x $HOME/public_html ; then
   # nothing
   echo nothing > /dev/null
else 
   mkdir $HOME/public_html >/dev/null 2>&1
   chmod 755 $HOME/public_html 2>&1
fi

if test -x $DOCROOT/$UserName ; then
   # nothing
   echo nothing > /dev/null
else
   ln -s $HOME/public_html $DOCROOT/$UserName 2> /dev/null
   chown $UserName $DOCROOT/$UserName
   chmod 755 $DOCROOT/$UserName
fi

#
# USERDOCROOT is users documents location
#

USERDOCROOT=$HOME/public_html

mkdir $USERDOCROOT/Public 2> /dev/null
chown $UserName $USERDOCROOT/Public
chmod 755 $USERDOCROOT/Public

mkdir $USERDOCROOT/Miscellaneous 2> /dev/null
chown $UserName $USERDOCROOT/Miscellaneous
chmod 755 $USERDOCROOT/Miscellaneous

#_____________________________________
#
#	update the list of all users for
#	the server
#_____________________________________

FACE=$USERDOCROOT/default.gif
if test -r /usr/local/lib/faces/$UserName ; then
	if test -x $TOGIF ; then
           $TOGIF /usr/local/lib/faces/$UserName $FACE
        else 
         cp $DOCROOT/default.gif $USERDOCROOT/default.gif
        fi
elif test -r /usr/lib/faces/$UserName ; then
    if test -x $TOGIF ; then
           $TOGIF /usr/lib/faces/$UserName $FACE
        else 
         cp $DOCROOT/default.gif $USERDOCROOT/default.gif
        fi
else
	cp $DOCROOT/default.gif $USERDOCROOT/default.gif
fi

#_____________________________________
#
# echo the home page.
#_____________________________________

if test -f $USERDOCROOT/index.html ; then
   USER_INDEX_HTML_FILE=$USERDOCROOT/index.html.N
else
   USER_INDEX_HTML_FILE=$USERDOCROOT/index.html
fi

cat <<ENDOFTEXT > $USER_INDEX_HTML_FILE
<html>
<head>
	<title>
		Home page for $NAME
	</title>
</head>
<body bgcolor="#42426f" text="#ffffff" link="#ffff00" vlink="#93db70" alink="#cc3232" background=/images/logo.gif>
<h1 align=center>
	$NAME's Home Page
</h1>

<p>

<center>
<P><img src=/~$UserName/default.gif align=center>
<P>Send <a href="mailto:$UserName@$HOST">email to me.</a>
<P>My machine is: <a href="/cgi-bin/MachineInfo/cgi-bin/wrap/$UserName/">$HOST</a>
</center>

<hr size=3 width=80% noshade>

<p>
Here is some content that you could customize.
Note that even though this is just a boring paragraph,
you could use all the powerful features of HTML
including using images, fonts, lists etc.

<p>
This file is:<br>

<center><code>$USERDOCROOT/index.html</code></center>

<hr size=3 width=80% noshade>

<!--#exec cmd="$ROOT/cgi-bin/wrap /$UserName" -->

</body>
</html>
ENDOFTEXT

cat <<ENDOFTEXT >> $DOCROOT/userList.html
<tr>

        <td><img src=/~$UserName/default.gif></td>

	<th align=right>
		<font size=+2>
		<a href="/~$UserName">$UserName</a>
		</font>
	</th>

	<td>
		<li>
	</td>

	<td><font size=+2><a href="/~$UserName">$NAME</a></font></td>
</tr>
ENDOFTEXT
